import java.io.IOException;
import java.io.InputStream;

public class MyHttpRequest {
    //接收连接对象的输入流并解析
    private InputStream inputStream;
    //目标资源的路径
    private  String uri;

    public MyHttpRequest(InputStream inputStream) {
        this.inputStream = inputStream;
    }
    //解析输入流
    public void parse(){
        try {
            byte[] bytes=new byte[1024];
            //把输入流中的东西读到数组中
            inputStream.read(bytes);
            //封装成字符串
            /*
            GET /a.html HTTP/1.1
            Host: localhost:8080
            Connection: keep-alive
            sec-ch-ua: "Chromium";v="106", "Microsoft Edge";v="106", "Not;A=Brand";v="99"
            sec-ch-ua-mobile: ?0
            sec-ch-ua-platform: "Windows"
            Upgrade-Insecure-Requests: 1
            User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36 Edg/106.0.1370.47
            Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*`............
            */
            String request=new String(bytes);
            //获取请求的目标资源路径，如http://localhost:8080/a.html中的a.html
            parseUri(request);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public void parseUri(String request){
        int index1,index2;
        index1=request.indexOf(' ');
        index2=request.indexOf(' ',index1+1);
        this.uri=request.substring(index1+1,index2);
        System.out.println(uri);
    }
    //返回uri
    public String getUri(){
        return this.uri;
    }
}
